﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using RooHui.Infrastructure.Entities.Entity;
using RooHui.Infrastructure.Entities.EFContext;
using RooHui.Tool;
using RooHui.Infrastructure.Entities.Repository;

namespace RooHui.OA.Biz
{
    public class UserRoleBiz
    {
        /// <summary>
        /// 获取用户角色
        /// </summary>
        /// <param name="userName"></param>
        /// <returns></returns>
        public static List<UserRole> GetUserRoleByUserName(string userName)
        {
            try
            {
                using (Context ctx = new Context())
                { 
                    var data = (from c in ctx.Role
                                join di in ctx.UserRole.Where(f => f.UserName == userName)
                                on c.ID equals di.RoleID into dt
                                from d in dt.DefaultIfEmpty()
                                select new { c, d })
                              .ToList()
                              .Select(e => new UserRole().Set(e.d).LoadRole(e.c))
                              .ToList();
                  
                    return data;
                }
            }
            catch (Exception ex)
            {
                LogHelper.Write(CommonLogger.Web, ex);
            }
            return null;
        }

        public static bool UserAuthorizeDelete(string userName)
        {
            bool isTrue = true;
            try
            {
                using (Context ctx = new Context())
                {
                    int count = ctx.UserRole.Count(c => c.UserName == userName);
                    if (count == 0)
                    {
                        return true;
                    }
                    int row = ctx.Delete<U_UserRole>()
                        .WhereSet(c => c.UserName, WhereOperator.Equal, userName)
                        .End()
                        .Excute();
                    if (row > 0)
                    {
                        isTrue = true;
                    }
                    else
                    {
                        isTrue = false;
                    }
                }
            }
            catch (Exception ex)
            {
                LogHelper.Write(CommonLogger.Web, ex);
                isTrue = false;
            }
            return isTrue;
        }

        public static bool UserAuthorize(string userName, string roleIDs)
        {
            bool isTrue = true;
            try
            {
                using (Context ctx = new Context())
                {
                    foreach (var roleID in roleIDs.Split(','))
                    {
                        var userRole = new UserRole()
                        {
                            UserName = userName,
                            RoleID = Convert.ToInt32(roleID)
                        };
                        userRole.PostAdd();
                    }
                }
            }
            catch (Exception ex)
            {
                LogHelper.Write(CommonLogger.Web, ex);
                isTrue = false;
            }
            return isTrue;
        }
    }
}
